package com.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.configs.RedisKeys;
import com.study.dao.BusinessMapper;
import com.study.pojo.entity.Business;
import com.study.pojo.resp.ResultMessage;
import com.study.service.BusinessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    BusinessMapper businessMapper;

    private static final Logger logger = LoggerFactory.getLogger(BusinessServiceImpl.class);
        @Override
        public ResultMessage findAllBus() {
            logger.debug("进入到新闻查询");
            //判断redis中是否有该key值  是否有新闻内容
            Boolean aBoolean = redisTemplate.hasKey(RedisKeys.BUSINESS);
            if (!aBoolean){
                //Redis中没有去查询数据库
                QueryWrapper<Business> w = new QueryWrapper<>();
                List<Business> businesses = businessMapper.selectList(w);
                //非空判断
                if (businesses!=null&&businesses.size()>0){
                    //存储进Redis中
                    redisTemplate.opsForList().leftPushAll(RedisKeys.BUSINESS,businesses);
                    logger.debug("添加完成:"+new Date());
                }else {
                    //防止内存穿透
                    List list = new ArrayList<>();
                    list.add("");
                    redisTemplate.opsForList().leftPushAll(RedisKeys.BUSINESS,list);
                    redisTemplate.expire(RedisKeys.BUSINESS,60, TimeUnit.SECONDS);
                }
            }
            //从redis中获取数据
            List range = redisTemplate.opsForList().range(RedisKeys.BUSINESS, 0, -1);
            System.out.println("range = " + range);
            if (StringUtils.isEmpty(range.toString())){
                return new ResultMessage().ERROR();
            }else{
                logger.debug("获取完成:"+new Date());
                return new ResultMessage(1,"ok",range,null);
            }
        }

        //根据新闻Id获取企业新闻
        @Override
        public ResultMessage findNewsByIdBus(Integer newsId) {
            logger.debug("进入到根据Id查询新闻接口");
            //判断Redis中是否有该key值
            //从Redis中获取新闻内容
            List<LinkedHashMap> range = redisTemplate.opsForList().range(RedisKeys.BUSINESS, 0, -1);
            Map o=null;
//        hutool
            for (LinkedHashMap o1 : range) {

                if (o1.get("businessId").equals(newsId)){
                    o =  o1;
                    break;
                }
            }
//        Object o = redisTemplate.opsForList().index(RedisKeys.JZG, newsId);
            //判空
            if (CollectionUtils.isEmpty(o)){
                Business business = businessMapper.selectById(newsId);
                return new ResultMessage(1,"ok",business,null);
            }else{
                logger.debug("查询完成:"+new Date());
                return new ResultMessage(1,"ok",o,null);
            }
        }

}
